/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package controller;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author pipo
 */
public class SaveLoadManager {
    
    private String userName;
    private mainGame.Map map;
    private int selection;
    private File saveFile;
    
    public SaveLoadManager(mainGame.Map map, int selection)
    {
        this.map = map;    
        this.userName = map.returnPlayer().getName();
        this.selection = selection;
        this.saveFile = new File(System.getProperty("user.dir"), userName + ".sf");
        try {
            this.saveFile.createNewFile();
        } catch (IOException ex) {
            Logger.getLogger(SaveLoadManager.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    public void writeSaveFile()
    {
        
        try {
            PrintWriter writer = new PrintWriter(this.saveFile, "UTF-8");
            writeMapState(writer);
            writer.close();
        } catch (FileNotFoundException ex) {
            Logger.getLogger(SaveLoadManager.class.getName()).log(Level.SEVERE, null, ex);
        } catch (UnsupportedEncodingException ex) {
            Logger.getLogger(SaveLoadManager.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        
        
    }
    
    public void writeMapState(PrintWriter writer)
    {

        writer.println("" + this.selection);
        writer.println(this.map.noOfItems());
        mainGame.Trainer t;
        mainGame.Pokemon p;
        mainGame.Trap trap;
        for(int i = 0 ; i < this.map.noOfItems() ; i++)
        {
            t = this.map.returnTrainer(i);
            writer.println(t.getName());
            writer.println(t.getXCoor());
            writer.println(t.getYCoor());
            if(i == 0)
            {
                mainGame.Player player = this.map.returnPlayer();
                writer.println(player.getScore()); //writer player's score
            }
            writer.println(t.pokemonCount());
            for(int j = 0 ; j < t.pokemonCount() ; j++)
            {
                p = t.returnPokemon(j);
                writer.println(p.getName());
                writer.println(p.getType());
                writer.println(p.getLevel());
                writer.println(p.getRawPower());
                writer.println(p.getRawDefense());
                writer.println(p.getHitPoints());
                writer.println(p.getMaxHitPoints());
                writer.println(p.getExperience());
                writer.println(p.getNextLevelExperience());
            }
        }
        //write traps
        writer.println(this.map.noOfTraps());
        for(int i = 0 ; i < this.map.noOfTraps() ; i++)
        {
            trap = this.map.returnTrap(i);
            writer.println(trap.getName());
            writer.println(trap.getXCoor());
            writer.println(trap.getYCoor());
            writer.println(trap.IsRevealed());
            p = trap.returnPokemon(0);
            writer.println(p.getName());
            writer.println(p.getType());
            writer.println(p.getLevel());
            writer.println(p.getRawPower());
            writer.println(p.getRawDefense());
            writer.println(p.getHitPoints());
            writer.println(p.getMaxHitPoints());
            writer.println(p.getExperience());
            writer.println(p.getNextLevelExperience());
        }
        
    }
    
    public static mainGame.Map loadMap(String userName)
    {
        
//(int X , int Y , String name)
		try {
			File file = new File(userName + ".sf");
			FileReader fileReader = new FileReader(file);
			BufferedReader bufferedReader = new BufferedReader(fileReader);
			String line = bufferedReader.readLine(); //no of trainers
                        int PlayerSelection = Integer.parseInt(line);
                        mainGame.Map loadMap = new mainGame.Map(26, 21 , userName , PlayerSelection , false);
                        line = bufferedReader.readLine();                        
                        int noofTrainers = Integer.parseInt(line);
                        int coorX , coorY;
                        int noOfPokemons;
                        int type, level , RP , RD , HP , MaxHP , EXP , NextExp;
                        int temp;
                        String trapName;
                        String name;
                        mainGame.Trainer trainer = null;
                        mainGame.Player player = null;
                        mainGame.Pokemon addPok;
                        mainGame.Trap trap;
			for(int i = 0 ; i < noofTrainers; i++) 
                        {
                            name = bufferedReader.readLine(); //user name
                            line = bufferedReader.readLine(); //coor x
                            coorX = Integer.parseInt(line);
                            line = bufferedReader.readLine(); // coor y
                            coorY = Integer.parseInt(line);
                            if(i == 0)
                            {
                                line = bufferedReader.readLine();
                                temp  = Integer.parseInt(line); //score of the player
                                player = new mainGame.Player(coorX, coorY, name, temp);
                                loadMap.addItemToMap(player);
                                loadMap.setPlayer(player);
                            }
                            
                            else
                            {
                              trainer = new mainGame.Trainer(coorX, coorY , name);
                              loadMap.addItemToMap(trainer);
                            }

                            line = bufferedReader.readLine(); //no of pokemons
                            temp = Integer.parseInt(line);
                            for(int j = 0 ; j < temp ; j++)
                            {
                                name = bufferedReader.readLine(); //pokemon name
                                line = bufferedReader.readLine();
                                type  = Integer.parseInt(line);  
                                line = bufferedReader.readLine();
                                level  = Integer.parseInt(line); 
                                line = bufferedReader.readLine();
                                RP  = Integer.parseInt(line); 
                                line = bufferedReader.readLine();
                                RD  = Integer.parseInt(line); 
                                line = bufferedReader.readLine();
                                HP  = Integer.parseInt(line); 
                                line = bufferedReader.readLine();
                                MaxHP  = Integer.parseInt(line); 
                                line = bufferedReader.readLine();
                                EXP  = Integer.parseInt(line); 
                                line = bufferedReader.readLine();
                                NextExp  = Integer.parseInt(line);
                                addPok = new mainGame.Pokemon(RP, RD, MaxHP, type, level, name);
                                addPok.setHitPoints(HP);
                                addPok.setExperience(EXP);
                                addPok.setNextLevelExperience(NextExp);
                                
                                
                                if(i == 0)
                                    player.addPokemon(addPok);
                                else
                                    trainer.addPokemon(addPok);
                                
                                
                            }

                            
			}
                        
                        boolean reveal;
                        line = bufferedReader.readLine();
                        temp = Integer.parseInt(line); // no of traps
                        for(int i = 0 ; i < temp ; i++)
                        {
                            trapName = bufferedReader.readLine(); //name of trap
                            line = bufferedReader.readLine();
                            coorX = Integer.parseInt(line);
                            line = bufferedReader.readLine();
                            coorY = Integer.parseInt(line);
                            line = bufferedReader.readLine(); //is revealed
                            reveal = Boolean.parseBoolean(line);
                            
                            name = bufferedReader.readLine(); //pokemon name
                            line = bufferedReader.readLine();
                            type  = Integer.parseInt(line);  
                            line = bufferedReader.readLine();
                            level  = Integer.parseInt(line); 
                            line = bufferedReader.readLine();
                            RP  = Integer.parseInt(line); 
                            line = bufferedReader.readLine();
                            RD  = Integer.parseInt(line); 
                            line = bufferedReader.readLine();
                            HP  = Integer.parseInt(line); 
                            line = bufferedReader.readLine();
                            MaxHP  = Integer.parseInt(line); 
                            line = bufferedReader.readLine();
                            EXP  = Integer.parseInt(line); 
                            line = bufferedReader.readLine();
                            NextExp  = Integer.parseInt(line);
                            addPok = new mainGame.Pokemon(RP, RD, MaxHP, type, level, name);
                            addPok.setHitPoints(HP);
                            addPok.setExperience(EXP);
                            addPok.setNextLevelExperience(NextExp);
                            
                            trap = new mainGame.Trap(coorX , coorY , addPok);
                            if(reveal)
                                trap.reveal();
                            loadMap.addTrapToMap(trap);
                            
                        }
			fileReader.close();
                        return loadMap;
		} catch (IOException e) {
			e.printStackTrace();
                        return null;
		}
	
    }
    
    
}
